package com.zby.demo.controller.front;

import com.zby.demo.dao.*;
import com.zby.demo.entity.*;
import com.zby.demo.entity.Class;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/21 21:04
 */

@Controller
public class TeaController {
    @Autowired
    NoticeDao noticeDao;
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    ClassDao classDao;
    @Autowired
    StudentDao studentDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:17
     * @Description: 前台教师首页
     */
    @RequestMapping("/front/teacher")
    public String toFrontIndex(Model model) {
        List<Notice> notices = noticeDao.queryAllNotice();
        model.addAttribute("notices", notices);
        return "front/teacher/index";
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:19
     * @Description: 前台教师教授课程页面
     */
    @RequestMapping("/front/teacher/courseinfo")
    public String toCourseInfo(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        Course course = teacherDao.queryTeachCourseByID(loginUser);
        long time = new Date().getTime();
        if (time - course.getBeginDate().getTime() >= 0) {
            if (course.getEndDate().getTime() + 1000 * 60 * 60 * 24 > time) {
                course.setStatus("进行中");
            } else {
                course.setStatus("已结束");
            }
        } else {
            course.setStatus("未开始");
        }
        model.addAttribute("course", course);
        List<Student> students = courseDao.queryCourseStudentsByID(course.getCourseId());
        model.addAttribute("students", students);
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "front/teacher/courseinfo";
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:22
     * @Description: 前台教师输入选报课程的学生的成绩
     */
    @RequestMapping("/front/teacher/enterscore")
    public String toEnterScore(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        Course course = teacherDao.queryTeachCourseByID(loginUser);
        model.addAttribute("course", course);
        List<Student> students = courseDao.queryCourseStudentsByID(course.getCourseId());
        model.addAttribute("students", students);
        List<Double> score1s = courseDao.queryCourseStudentScore1s(course.getCourseId());
        model.addAttribute("score1s", score1s);
        List<Double> score2s = courseDao.queryCourseStudentScore2s(course.getCourseId());
        model.addAttribute("score2s", score2s);
        return "front/teacher/enterscore";
    }

    /*
     * @Param:
     * @Return:
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:23
     * @Description: 更新选报课程学生成绩信息
     */
    @PostMapping("/front/teacher/enterscore")
    public String updateStudentSumScore(@Param("courseId") String courseId, @Param("enterscore") String enterscore) {
        String[] scores = enterscore.split(";");
        for (int i = 0; i < scores.length; i++) {
            String[] stu = scores[i].split(",");
            courseDao.updateStudentScore(courseId, stu[0], stu[1], stu[2]);
        }
        return "redirect:/front/teacher/enterscore";
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:26
     * @Description: 前台教师信息
     */
    @RequestMapping("/front/teacher/info")
    public String toInfo(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        Teacher teacher = teacherDao.quertyTeacherByID(loginUser);
        model.addAttribute("tea", teacher);
        Course course = teacherDao.queryTeachCourseByID(loginUser);
        model.addAttribute("course", course);
        return "front/teacher/info";
    }

    /*
     * @Param: [teacher]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:34
     * @Description: 更改教师信息
     */
    @PostMapping("/front/teacher/info")
    public String updateInfo(Teacher teacher) {
        teacherDao.updateTeacherInfo(teacher);
        return "redirect:/front/teacher/info";
    }

    /*
     * @Param: [teacherId, password]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:34
     * @Description: 修改教师密码
     */
    @PostMapping("/front/teacher/info/{teacherId}")
    public String updatePassword(@PathVariable("teacherId") String teacherId, @Param("password") String password) {
        teacherDao.updateTeacherPassword(teacherId, password);
        return "redirect:/logout";
    }
}
